package com.china.api.web.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.china.itapi.common.entity.Contype;

import static org.hibernate.criterion.Example.create;

/**
 * A data access object (DAO) providing persistence and search support for
 * Contype entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fdway.po.Contype
 * @author MyEclipse Persistence Tools
 */

public class ContypeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ContypeDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String BACK1 = "back1";
	public static final String BACK2 = "back2";
	public static final String BACK3 = "back3";

	public void save(Contype transientInstance) {
		
		log.debug("saving Contype instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Contype persistentInstance) {
		log.debug("deleting Contype instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Contype findById(java.lang.Long id) {
		log.debug("getting Contype instance with id: " + id);
		try {
			Contype instance = (Contype) getSession().get(
					"com.fdway.po.Contype", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Contype> findByExample(Contype instance) {
		log.debug("finding Contype instance by example");
		try {
			List<Contype> results = (List<Contype>) getSession()
					.createCriteria("com.fdway.po.Contype").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Contype instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Contype as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Contype> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Contype> findByBack1(Object back1) {
		return findByProperty(BACK1, back1);
	}

	public List<Contype> findByBack2(Object back2) {
		return findByProperty(BACK2, back2);
	}

	public List<Contype> findByBack3(Object back3) {
		return findByProperty(BACK3, back3);
	}

	public List findAll() {
		log.debug("finding all Contype instances");
		try {
			String queryString = "from Contype";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findMyAll(long back1) {
		log.debug("finding all Contype instances");
		try {
			String queryString = "from Contype c where c.id =? ";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, back1);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Contype merge(Contype detachedInstance) {
		log.debug("merging Contype instance");
		try {
			Contype result = (Contype) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Contype instance) {
		log.debug("attaching dirty Contype instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Contype instance) {
		log.debug("attaching clean Contype instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}